home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_11_06
/
suer01.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-04-10
|
27KB
|
813 lines
/*****************************************************/
/* NATURAL.C Copyright (c) 1993 Russell Suereth */
/*****************************************************/
/*****************************************************/
/* This is the original natural language processor */
/* code plus expansions for tense and number. */
/*****************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "natural.h"
void initialize(void);
void reset_sentence(void);
void get_record(char *);
char *extract_word(void);
int match_record(char *, int);
char *extract_root(void);
int check_underlying(void);
int check_type(char *,int);
void get_aux(void);
int match_aux(void);
void check_subject(void);
void check_action(void);
void check_place(void);
void check_aux_verb(void);
void check_number(void);
void make_response(void);
void make_answer(int);
void get_verb(char, char, char);
int match_verb(char, char, char);
FILE *infile;
char dic_record[80];
int sentence;
int word_ct;
char word_array[10][25];
char root_array[10][25];
char prime_types[10][11];
char phrases[10][11];
char type_array[10][5][11];
char subjects[20][25];
char actions[20][25];
char places[20][31];
char response[200];
unsigned char verb_tense[5];
unsigned char verb_number[5];
unsigned char verb_usage;
unsigned char aux_tense[5];
unsigned char aux_number[5];
unsigned char aux_usage;
unsigned char subject_number;
unsigned char tenses[20];
unsigned char numbers[20];
unsigned char usages[20];
unsigned char subjects_type[20];
unsigned char aux_meaning[20][5];
char auxiliaries[20][25];
void main()
{
char *cur_word;
char in_sentence[80];
initialize();
if ((infile = fopen("diction", "r+")) == NULL) {
printf ("\nError opening dictionary\n");
exit(0);
}
printf("\nSentence: ");
while(gets(in_sentence)) {
if (in_sentence[0] == '\0') break;
reset_sentence();
cur_word = strtok(in_sentence, " ");
while(cur_word != NULL) {
get_record(cur_word);
cur_word = strtok(NULL, " ");
if (++word_ct > 9) break;
}
if (check_underlying() == 0) {
check_subject();
check_action();
check_place();
check_aux_verb();
check_number();
}
make_response();
printf("Response: %s\n\nSentence: ", response);
if (++sentence > 19) break;
} /* end while */
fclose(infile);
return;
}
/*****************************************************/
/* Initialize variables. */
/*****************************************************/
void initialize()
{
sentence = 0;
memset(subjects, '\0', 500);
memset(actions, '\0', 500);
memset(places, '\0', 620);
memset(tenses, '\0', 20);
memset(numbers, '\0', 20);
memset(usages, '\0', 20);
memset(subjects_type, '\0', 20);
memset(aux_meaning, '\0', 100);
memset(auxiliaries, '\0', 500);
return;
}
/*****************************************************/
/* These variables are initialized for each new */
/* input sentence. */
/*****************************************************/
void reset_sentence()
{
word_ct = 0;
memset(word_array, '\0', 250);
memset(root_array, '\0', 250);
memset(prime_types, '\0', 110);
memset(phrases, '\0', 110);
memset(type_array, '\0', 550);
response[0] = '\0';
return;
}
/*****************************************************/
/* Get all the records from the dictionary. If the */
/* passed word is not in the dictionary, then the */
/* word could be a name. */
/*****************************************************/
void get_record(char *pass_word)
{
int types = 0;
rewind (infile);
fgets(dic_record, 80, infile);
while (! feof(infile)) {
if (match_record(pass_word, types) == 0)
types++;
fgets(dic_record, 80, infile);
}
if (types == 0) {
if (isupper( (int) pass_word[0]))
strcpy(type_array[word_ct][types], "NAME");
else
strcpy(type_array[word_ct][types],
"NOTFOUND");
}
strcpy(word_array[word_ct], pass_word);
return;
}
/*****************************************************/
/* Compare the passed word with the word in the */
/* current dictionary record. If they are the same, */
/* then extract the type (NOUN, VERB, etc.). If the */
/* type is PRON, then extract pronoun information. */
/* If the type is VERB, then extract verb */
/* information. */
/*****************************************************/
int match_record(char *pass_word, int types)
{
int i, j;
char *root;
char *dic_word;
dic_word = extract_word();
/* Check if passed word equals dictionary word */
if (strcmpi(pass_word, dic_word) != 0) return(1);
/* Word found, get the type */
for (i=24,j=0; i<28; i++) {
if (isspace(dic_record[i])) break;
type_array[word_ct][types][j++] = dic_record[i];
}
/* Trim the type */
type_array[word_ct][types][j] = '\0';
if (strcmp(type_array[word_ct][types],
"PRON") == 0)
subject_number = dic_record[41];
if (strcmp(type_array[word_ct][types],
"VERB") == 0) {
root = extract_root();
strcpy(root_array[word_ct], root);
verb_usage = dic_record[29];
for (i=30,j=0; i<34; i++,j++) {
if (isspace(dic_record[i])) break;
verb_tense[j] = dic_record[i];
}
verb_tense[j] = '\0';
for (i=41,j=0; i<43; i++,j++) {
if (isspace(dic_record[i])) break;
verb_number[j] = dic_record[i];
}
verb_number[j] = '\0';
}
return(0);
}
/*****************************************************/
/* Extract the word from the dictionary. The word is */
/* 24 characters in length and starts in column 1. */
/*****************************************************/
char *extract_word()
{
int i;
char dic_word[25];
strncpy(dic_word, dic_record, 24);
for (i=23; i>=0; i--) {
if (isspace(dic_word[i])) {
dic_word[i] = '\0';
continue;
}
break;
}
return(dic_word);
}
/*****************************************************/
/* Extract the root from the dictionary. It */
/* identifies a group of similar words (the root for */
/* run, ran, runs and running is run). It is 14 */
/* characters in length and starts in column 47. */
/*****************************************************/
char *extract_root()
{
int i, j;
char root[15];
for (i=46,j=0; i<60; i++) {
if (isspace(dic_record[i])) break;
root[j++] = dic_record[i];
}
/* Trim the root */
root[j] = '\0';
return(root);
}
/*****************************************************/
/* Determine if the input sentence contains a known, */
/* underlying structure. If it does, then assign the */
/* correct types and phrases for the words. */
/*****************************************************/
int check_underlying()
{
int i = 0;
/* Structure WH-AUX-PRON-VERB */
if ( (check_type("WH", i) == 0) &&
(check_type("AUX", i+1) == 0) &&
(check_type("PRON", i+2) == 0) &&
(check_type("VERB", i+3) == 0) ) {
strcpy(prime_types[i], "WH");
strcpy(prime_types[i+1], "AUX");
strcpy(prime_types[i+2], "PRON");